package com.klondike.common.io;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public interface FileVisitor {

    void visit(File file) throws IOException;

    FileFilter getFilter();

    abstract class JarEntryVisitor implements FileVisitor {
        private final FileFilter filter;

        public JarEntryVisitor() {
            this(".jar");
        }

        public JarEntryVisitor(String fileNamePrefix) {
            filter = IOUtils.fileNameSuffixFilter(fileNamePrefix);
        }

        @Override
        public void visit(File file) throws IOException {
            JarFile jarFile = new JarFile(file);
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                visit(jarFile, entries.nextElement());
            }
        }

        protected abstract void visit(JarFile jarFile, JarEntry e) throws IOException;

        @Override
        public FileFilter getFilter() {
            return filter;
        }
    }
}
